<?php

namespace App\Admin\Controllers\Scaffold;

use App\Admin\Extensions\Scaffold\StaticTabCreator;
use App\Admin\Extensions\Widgets\StaticTab;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class StaticTabTableGeneratorController extends Controller
{

    /**
     * index page
     *
     * @return mixed
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('静态Tab Generator');


            $content->row(view('admin.scaffold.statictab_generator',
                               [
                                   'code' => ""
                               ]));
        });
    }

    public function test()
    {
        return Admin::content(function (Content $content) {

            $content->header('test');
            $content->description('test desc');

            $headers = ['Id', 'Email', 'Name', 'Company'];
            $rows = [
                [1, 'labore21@yahoo.com', 'Ms. Clotilde Gibson', 'Goodwin-Watsica'],
                [2, 'omnis.in@hotmail.com', 'Allie Kuhic', 'Murphy, Koepp and Morar'],
                [3, 'quia65@hotmail.com', 'Prof. Drew Heller', 'Kihn LLC'],
                [4, 'xet@yahoo.com', 'William Koss', 'Becker-Raynor'],
                [5, 'ipsa.aut@gmail.com', 'Ms. Antonietta Kozey Jr.'],
            ];

            $st = new StaticTab();
            $st->add('tab1','http://baidu.com');
            $st->add('tab2','http://qq.com');
            $st->add('tab3','http://sina.com');
            $st->content('!!!code here!!!');
            $st->active(0);

            $content->row(function (Row $row) use ($st) {
                $row->column(12, $st);
            });
        });
    }

    /**
     * generate code
     *
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function generate(Request $request)
    {
        try {

            // get params
            $controllerName = $request->get('controller_name');
            $modelName = $request->get('model_name');
            $header = $request->get('page_title');
            $desc = $request->get('page_desc');
            $fields = $request->get('fields');

            $content = (new StaticTabCreator($controllerName, $modelName, $header, $desc, $fields))->create();

        } catch (\Exception $exception) {

            $bundle = [
                'result' => 'failed',
                'code' => $exception->getTraceAsString(),
            ];

            return response()->json($bundle);
        }

        $bundle = [
            'result' => 'success',
            'code' => $content,
        ];

        return response()->json($bundle);
    }
}
